DROP FUNCTION IF EXISTS public."collection_userLogin"(date, date, integer, integer);

CREATE OR REPLACE FUNCTION public."collection_userLogin"("fromDate" date DEFAULT NULL::date, "toDate" date DEFAULT NULL::date, accountid integer DEFAULT NULL::integer, locationid integer DEFAULT NULL::integer)
 RETURNS TABLE("AccountId" integer, "ReceiptCreatedBy" text, "RoleName" character varying, "ReceiptDate" timestamp without time zone, "ReceiptId" integer, "ReceiptTypeId" integer, "AreaType" character varying, "RefId" character varying, "PayTypeName" character varying, "PaymentDetails" character varying, "PaidAmount" numeric, "PatientName" text, "UMRNo" character varying, "Mobile" character varying)
 LANGUAGE plpgsql
AS $function$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
JOIN "LocationAccountMap" LAM on LAM."AccountId"=a."AccountId"  
join "Role" rol on rol."RoleId" = a."RoleId" and lower(rol."RoleName") <> lower('Patient')
where a."RoleId"<>4  --and a."AccountId"=6776 and LAM."LocationId"=2
 and	case when accountid is null then 1=1 else a."AccountId"=accountid end   
AND CASE WHEN (locationid IS NULL OR locationid=0) THEN 1=1 ELSE LAM."LocationId"=locationid END
)
, appointment as (

select R."ReceiptId",A."FullName" "ReceiptCreatedBy",Rl."RoleName",R."CreatedDate" "ReceiptDate",R."CreatedBy",RT."Name" as "AreaType"
, R."Cost"  as "PaidAmount" ,R."ReceiptTypeId"
 ,Ad."FollowUpForAppointmentId",
	Ad."AppointmentDate",Ad."AppointmentNo", Ad."AppointmentTime",Ad."LocationId",
pa."FullName" "PatientName",Pa."UMRNo",pa."Mobile",pr."FullName" as "ProviderName",Ad."PaymentType",
pt."PayTypeName",R."PaymentDetails" , R."AppointmentId",R."IsAppointmentReceipt" 
	from "Receipt" R
join "Appointment" Ad on Ad."AppointmentId"=R."RespectiveId" 
join "Provider" pr on pr."ProviderId"= ad."ProviderId"
join "Patient" pa on Ad."PatientId"=pa."PatientId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" Rl on Rl."RoleId"=A."RoleId"
join "PayType" pt on pt."PayTypeId"=R."PayTypeId"
join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
	where  R."Active" <> false  and R."ReceiptAreaTypeId" in (4,5) and
	--R."CreatedBy" =6776 and Ad."LocationId" = 2
--and R."CreatedDate"::date ='2023-04-18'
 case when "accountid" is null then 1=1 else  R."CreatedBy"  = "accountid" end  
 and case when "locationid" is null then 1=1 else Ad."LocationId" = "locationid" end

 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end 
 
)
,patient as(			
						select 
                                R."Cost" as "PaidAmount", 
	
                                A."FullName" as "ReceiptCreatedBy",Rl."RoleName",
                                P."FullName" as "PatientName",P."FirstName",P."MiddleName",P."LastName",
                                P."Age",P."Gender",P."UMRNo",P."Mobile",P."DateOfBirth",
                                R."ReceiptId",R."CreatedBy",RT."Name" as "AreaType",P."PatientId",
				                PT."PayTypeName",R."PaymentDetails",R."CreatedDate" "ReceiptDate"	,R."ReceiptTypeId"
	                            FROM "Receipt" R
	                            join "Patient" P on P."PatientId" = R."RespectiveId"  
								join  "Account" PA on PA."ReferenceId"=P."PatientId"
								JOIN "LocationAccountMap" LAM on LAM."AccountId"=PA."AccountId"  
	                            join "Account" A on A."AccountId" = R."CreatedBy"
						        join "Role" Rl on Rl."RoleId"=A."RoleId"
	                            join "PayType" PT on PT."PayTypeId" = R."PayTypeId"
							join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
								where  R."ReceiptAreaTypeId" in (3,6)
--	and LAM."LocationId"=2 and R."CreatedDate"::date='2023-04-14 and R."CreatedBy"=6776
									
 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
 	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
	case when "locationid" is null then 1=1 else  LAM."LocationId" = "locationid"::int end and
	case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end 
	
)

,admission as (
	
select R."ReceiptId",Rl."RoleName",R."CreatedDate" "ReceiptDate",
A."FullName" "ReceiptCreatedBy" ,pt."PayTypeName",R."PaymentDetails"
,R."CreatedBy",RT."Name" as "AreaType",R."ReceiptTypeId",
 R."Cost"  as "PaidAmount" 
 ,Ad."AdmissionNo",ad."AdmissionDate",	
pa."FullName" "PatientName",pa."UMRNo",pa."Mobile",pr."FullName" as "ProviderName"

	from "Receipt" R
join "Admission" ad on Ad."AdmissionId"=R."RespectiveId" and ad."Active" is not false
join "Provider" pr on pr."ProviderId"= ad."ProviderId"
join "Patient" pa on Ad."PatientId"=pa."PatientId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" Rl on Rl."RoleId"=A."RoleId"
join "PayType" pt on pt."PayTypeId"=R."PayTypeId"
	join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
	where R."Active" is not false and R."ReceiptAreaTypeId" in (12,13) and
	--and  Ad."LocationId"=2 and  R."CreatedBy"=6776
case when "accountid" is null then 1=1 else  R."CreatedBy"  = "accountid" end 
and case when "locationid" is null then 1=1 else Ad."LocationId" = "locationid" end 
and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end  

)
,lab as (
-- 	select 
--  R."Cost" as "PaidAmount",PT."PayTypeName",R."PaymentDetails", 
-- 	 AR."FullName" "ReceiptCreatedBy",Rl."RoleName",R."CreatedBy",
-- 	 nlbh."RequisitionNumber",	
-- 	 nlbh."PatientId", nlbh."DoctorId",
-- 	COALESCE (nlbh."PatientName" ,p."FullName" ) as "PatientName",COALESCE (p."Mobile" ,nlbh."Mobile" ) as "Mobile", p."UMRNo",           
--       R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate"	,R."ReceiptTypeId"

SELECT 
	 R."Cost" as "PaidAmount",PT."PayTypeName",R."PaymentDetails", 
	 AR."FullName" "ReceiptCreatedBy",Rl."RoleName",R."CreatedBy",
	 nlbh."RequisitionNumber",	
	 nlbh."PatientId", nlbh."DoctorId",
	COALESCE (nlbh."PatientName" ,p."FullName" ) as "PatientName",COALESCE (p."Mobile" ,nlbh."Mobile" ) as "Mobile", p."UMRNo",           
      R."ReceiptId",RT."Name" as "AreaType" ,R."CreatedDate" "ReceiptDate"	,R."ReceiptTypeId"
			 from "Receipt" R
			join "NewLabBookingHeader" nlbh on nlbh."NewLabBookingHeaderId"=R."RespectiveId"                                
			join "PayType" pt ON PT."PayTypeId" = nlbh."PayTypeId"                                                                      
			left join "Patient" p ON p."PatientId" =nlbh."PatientId"
			left join "Provider" prv on prv."ProviderId" = nlbh."DoctorId"                                
			left join "Account" e on e."AccountId" = nlbh."EmployeeId" 
			left join "Account" AR on AR."AccountId" = R."CreatedBy"
			join "Role" Rl on Rl."RoleId"=AR."RoleId"
			join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
		where R."ReceiptAreaTypeId" = 8
		--and nlbh."LocationId"=2 and R."CreatedDate"::date='2023-04-18'
 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
 	case when "locationid" is null then 1=1 else  nlbh."LocationId" = "locationid"::int end and
	case when "accountid" is null then 1=1 else nlbh."CreatedBy"="accountid" end 

UNION
		
SELECT 
	 R."Cost" as "PaidAmount", PT."PayTypeName",R."PaymentDetails",
	 AR."FullName" "ReceiptCreatedBy" ,Rl."RoleName",R."CreatedBy",
	 nlbh."RequisitionNumber",
	 nlbh."PatientId", nlbh."DoctorId", 
	COALESCE (nlbh."PatientName" ,p."FullName" ) as "PatientName",COALESCE (p."Mobile" ,nlbh."Mobile" ) as "Mobile", p."UMRNo",           
      R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate"	,R."ReceiptTypeId"
			
	
		from "NewLabBookingHeader" nlbh
	join "NewLabCancelBookingHeader" SH on SH."NewLabBookingHeaderId"=nlbh."NewLabBookingHeaderId"
	join "Receipt" R on R."RespectiveId"= SH."NewLabCancelBookingHeaderId"	
			join "PayType" pt ON PT."PayTypeId" = nlbh."PayTypeId"                                                                      
			left join "Patient" p ON p."PatientId" =nlbh."PatientId"		
			left join "Account" AR on AR."AccountId" = R."CreatedBy"
			join "Role" Rl on Rl."RoleId"=AR."RoleId"
			join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"
		where R."ReceiptAreaTypeId" = 9
		--and nlbh."LocationId"=2 and R."CreatedDate"::date='2023-04-18'
 	and case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
 	case when "locationid" is null then 1=1 else  nlbh."LocationId" = "locationid"::int end and
	case when "accountid" is null then 1=1 else nlbh."CreatedBy"="accountid" end 
		
																		  								   
)

,pharma as (

select 
 R."Cost" as "PaidAmount", PT."PayTypeName",R."PaymentDetails",
	PH."PharmacySaleHeaderId" ,PH."BillNumber",
	A."FullName" "ReceiptCreatedBy",AR."RoleName" ,R."CreatedBy",
	PH."PatientName",PH."Mobile" ,Pa."UMRNo",
	 R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate",R."ReceiptTypeId"
	
	from "Receipt" R
		join "PharmacySaleHeader" PH on PH."PharmacySaleHeaderId"=R."RespectiveId"
		join "Account" A on A."AccountId"=R."CreatedBy"
		join "Role" AR on AR."RoleId"=A."RoleId"
		 join "PayType" PT ON PT."PayTypeId" = R."PayTypeId"  
		join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"	 	
		left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
	where R."ReceiptAreaTypeId"=1  
 		--and  PH."LocationId"=2 and R."CreatedDate"::date='2023-04-18' 	
	 and case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end and
	
 	case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
 	case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
  	case when "locationid" is null then 1=1 else PH."LocationId" = "locationid" end
UNION
	
select 
	 R."Cost" as "PaidAmount", PT."PayTypeName",R."PaymentDetails",
		PH."PharmacySaleHeaderId" ,PH."BillNumber",
		A."FullName" "ReceiptCreatedBy",AR."RoleName" ,R."CreatedBy",
		PH."PatientName",PH."Mobile" ,Pa."UMRNo",
		 R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate",R."ReceiptTypeId"
	from "PharmacySaleHeader" PH 
			join "SaleReturnHeader" SH on SH."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
			join "Receipt" R on R."RespectiveId"= SH."SaleReturnHeaderId"
		join "Account" A on A."AccountId"=R."CreatedBy"
		join "Role" AR on AR."RoleId"=A."RoleId"
		 join "PayType" PT ON PT."PayTypeId" = R."PayTypeId"  
		join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"	 	
		left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
	where R."ReceiptAreaTypeId"=7
	
		 and case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end and
		 case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
		case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
		case when "locationid" is null then 1=1 else PH."LocationId" = "locationid" end		
)
,scan as(

select 
		 R."Cost" as "PaidAmount", PT."PayTypeName",R."PaymentDetails",
			 PH."RequisitionNumber",
			 R."ReceiptId",RT."Name" as "AreaType",R."CreatedDate" "ReceiptDate",R."ReceiptTypeId",
			PA."FullName" as "PatientName",PA."Mobile" ,PA."UMRNo",
	R."CreatedBy",A."FullName" "ReceiptCreatedBy",AR."RoleName" 
	
		from "Receipt" R
		join "BookScanAppointment" PH on PH."BookScanAppointmentId"=R."RespectiveId" 
		join "Account" A on A."AccountId"=R."CreatedBy"
		join "Role" AR on AR."RoleId"=A."RoleId"
		 join "PayType" PT ON PT."PayTypeId" = R."PayTypeId"  
		join "ReceiptAreaType" RT on R."ReceiptAreaTypeId"=RT."ReceiptAreaTypeId"	
		left join "Patient" PA on PA."PatientId"=PH."PatientId"

	where  R."ReceiptAreaTypeId" in (10,11)
		--PH."LocationId"=2  and R."CreatedDate"::date='2023-04-14' 		
		and case when "accountid" is null then 1=1 else R."CreatedBy"="accountid" end and	
		case when "fromDate" is null then 1=1 else "fromDate"<= R."CreatedDate"::date end and
		case when "toDate" is null then 1=1 else R."CreatedDate"::date<=  "toDate" end and
		case when "locationid" is null then 1=1 else PH."LocationId" = "locationid" end
)
SELECT O."AccountId",O."ReceiptCreatedBy",O."RoleName",O."ReceiptDate",O."ReceiptId",O."ReceiptTypeId",
O."AreaType",O."RefId",
coalesce(O."PayTypeName"::character varying,'GRAND')"PayTypeName"
,O."PaymentDetails"
,sum(O."PaidAmount")"PaidAmount",--O."RefundAmount",O."BalanceAmount",sum(A."PaidAmount") "PaidAmount"
O."PatientName",O."UMRNo",O."Mobile"
from 
(
	Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."AppointmentNo" as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join appointment AP on AP."CreatedBy"=A."AccountId"
UNION
	Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."AdmissionNo" as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join admission AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."PatientId"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join patient AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."RequisitionNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join lab AP on AP."CreatedBy"=A."AccountId"
UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."BillNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join pharma AP on AP."CreatedBy"=A."AccountId"
	UNION
Select  A."AccountId",AP."ReceiptCreatedBy",AP."RoleName",AP."ReceiptDate",AP."ReceiptId",AP."ReceiptTypeId",
AP."AreaType",AP."RequisitionNumber"::character varying as "RefId",AP."PayTypeName",AP."PaymentDetails"
,AP."PaidAmount",--AP."RefundAmount",AP."BalanceAmount",
AP."PatientName",AP."UMRNo",AP."Mobile"
from accountdata A
join scan AP on AP."CreatedBy"=A."AccountId"	
	
)O



GROUP BY GROUPING SETS((O."AccountId",O."ReceiptCreatedBy",O."RoleName",O."ReceiptDate",
						O."ReceiptId",O."ReceiptTypeId",O."AreaType",O."RefId",O."PayTypeName",
						O."PaymentDetails",O."PatientName",O."UMRNo",O."Mobile"),(O."PayTypeName"), ())   



order by O."PayTypeName" asc;

end
$function$
;
